home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / MPW / flex 2.4.6 / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-21  |  14.3 KB  |  782 lines  |  [TEXT/MPS ]

  1. /* misc - miscellaneous flex routines */
  2.  
  3. /*-
  4.  * Copyright (c) 1990 The Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * This code is derived from software contributed to Berkeley by
  8.  * Vern Paxson.
  9.  * 
  10.  * The United States Government has rights in this work pursuant
  11.  * to contract no. DE-AC03-76SF00098 between the United States
  12.  * Department of Energy and the University of California.
  13.  *
  14.  * Redistribution and use in source and binary forms are permitted provided
  15.  * that: (1) source distributions retain this entire copyright notice and
  16.  * comment, and (2) distributions including binaries display the following
  17.  * acknowledgement:  ``This product includes software developed by the
  18.  * University of California, Berkeley and its contributors'' in the
  19.  * documentation or other materials provided with the distribution and in
  20.  * all advertising materials mentioning features or use of this software.
  21.  * Neither the name of the University nor the names of its contributors may
  22.  * be used to endorse or promote products derived from this software without
  23.  * specific prior written permission.
  24.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  25.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  26.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27.  */
  28.  
  29. /* $Header: misc.c,v 1.2 94/01/04 14:33:10 vern Exp $ */
  30.  
  31. #include "flexdef.h"
  32.  
  33.  
  34.  
  35. /* declare functions that have forward references */
  36.  
  37. void dataflush PROTO((void));
  38. int otoi PROTO((Char []));
  39.  
  40.  
  41. void add_action( new_text )
  42. char *new_text;
  43.     {
  44.     int len = strlen( new_text );
  45.  
  46.     while ( len + action_index >= action_size - 10 /* slop */ )
  47.         {
  48.         action_size *= 2;
  49.         action_array =
  50.             reallocate_character_array( action_array, action_size );
  51.         }
  52.  
  53.     strcpy( &action_array[action_index], new_text );
  54.  
  55.     action_index += len;
  56.     }
  57.  
  58.  
  59. /* allocate_array - allocate memory for an integer array of the given size */
  60.  
  61. void *allocate_array( size, element_size )
  62. int size, element_size;
  63.     {
  64.     register void *mem;
  65.  
  66.     /* On 16-bit int machines (e.g., 80286) we might be trying to
  67.      * allocate more than a signed int can hold, and that won't
  68.      * work.  Cheap test:
  69.      */
  70.     if ( element_size * size <= 0 )
  71.         flexfatal( "request for < 1 byte in allocate_array()" );
  72.  
  73.     mem = flex_alloc( element_size * size );
  74.  
  75.     if ( mem == NULL )
  76.         flexfatal( "memory allocation failed in allocate_array()" );
  77.  
  78.     return mem;
  79.     }
  80.  
  81.  
  82. /* all_lower - true if a string is all lower-case */
  83.  
  84. int all_lower( str )
  85. register char *str;
  86.     {
  87.     while ( *str )
  88.         {
  89.         if ( ! isascii( (Char) *str ) || ! islower( *str ) )
  90.             return 0;
  91.         ++str;
  92.         }
  93.  
  94.     return 1;
  95.     }
  96.  
  97.  
  98. /* all_upper - true if a string is all upper-case */
  99.  
  100. int all_upper( str )
  101. register char *str;
  102.     {
  103.     while ( *str )
  104.         {
  105.         if ( ! isascii( (Char) *str ) || ! isupper( *str ) )
  106.             return 0;
  107.         ++str;
  108.         }
  109.  
  110.     return 1;
  111.     }
  112.  
  113.  
  114. /* bubble - bubble sort an integer array in increasing order
  115.  *
  116.  * synopsis
  117.  *   int v[n], n;
  118.  *   void bubble( v, n );
  119.  *
  120.  * description
  121.  *   sorts the first n elements of array v and replaces them in
  122.  *   increasing order.
  123.  *
  124.  * passed
  125.  *   v - the array to be sorted
  126.  *   n - the number of elements of 'v' to be sorted
  127.  */
  128.  
  129. void bubble( v, n )
  130. int v[], n;
  131.     {
  132.     register int i, j, k;
  133.  
  134.     for ( i = n; i > 1; --i )
  135.         for ( j = 1; j < i; ++j )
  136.             if ( v[j] > v[j + 1] )    /* compare */
  137.                 {
  138.                 k = v[j];    /* exchange */
  139.                 v[j] = v[j + 1];
  140.                 v[j + 1] = k;
  141.                 }
  142.     }
  143.  
  144.  
  145. /* check_char - checks a character to make sure it's within the range
  146.  *        we're expecting.  If not, generates fatal error message
  147.  *        and exits.
  148.  */
  149.  
  150. void check_char( c )
  151. int c;
  152.     {
  153.     if ( c >= CSIZE )
  154.         lerrsf( "bad character '%s' detected in check_char()",
  155.             readable_form( c ) );
  156.  
  157.     if ( c >= csize )
  158.         lerrsf( "scanner requires -8 flag to use the character '%s'",
  159.             readable_form( c ) );
  160.     }
  161.  
  162.  
  163.  
  164. /* clower - replace upper-case letter to lower-case */
  165.  
  166. Char clower( c )
  167. register int c;
  168.     {
  169.     return (Char) ((isascii( c ) && isupper( c )) ? tolower( c ) : c);
  170.     }
  171.  
  172.  
  173. /* copy_string - returns a dynamically allocated copy of a string */
  174.  
  175. char *copy_string( str )
  176. register char *str;
  177.     {
  178.     register char *c;
  179.     char *copy;
  180.  
  181.     /* find length */
  182.     for ( c = str; *c; ++c )
  183.         ;
  184.  
  185.     copy = (char *) flex_alloc( (c - str + 1) * sizeof( char ) );
  186.  
  187.     if ( copy == NULL )
  188.         flexfatal( "dynamic memory failure in copy_string()" );
  189.  
  190.     for ( c = copy; (*c++ = *str++); )
  191.         ;
  192.  
  193.     return copy;
  194.     }
  195.  
  196.  
  197. /* copy_unsigned_string -
  198.  *    returns a dynamically allocated copy of a (potentially) unsigned string
  199.  */
  200.  
  201. Char *copy_unsigned_string( str )
  202. register Char *str;
  203.     {
  204.     register Char *c;
  205.     Char *copy;
  206.  
  207.     /* find length */
  208.     for ( c = str; *c; ++c )
  209.         ;
  210.  
  211.     copy = allocate_Character_array( c - str + 1 );
  212.  
  213.     for ( c = copy; (*c++ = *str++); )
  214.         ;
  215.  
  216.     return copy;
  217.     }
  218.  
  219.  
  220. /* cshell - shell sort a character array in increasing order
  221.  *
  222.  * synopsis
  223.  *
  224.  *   Char v[n];
  225.  *   int n, special_case_0;
  226.  *   cshell( v, n, special_case_0 );
  227.  *
  228.  * description
  229.  *   Does a shell sort of the first n elements of array v.
  230.  *   If special_case_0 is true, then any element equal to 0
  231.  *   is instead assumed to have infinite weight.
  232.  *
  233.  * passed
  234.  *   v - array to be sorted
  235.  *   n - number of elements of v to be sorted
  236.  */
  237.  
  238. void cshell( v, n, special_case_0 )
  239. Char v[];
  240. int n, special_case_0;
  241.     {
  242.     int gap, i, j, jg;
  243.     Char k;
  244.  
  245.     for ( gap = n / 2; gap > 0; gap = gap / 2 )
  246.         for ( i = gap; i < n; ++i )
  247.             for ( j = i - gap; j >= 0; j = j - gap )
  248.                 {
  249.                 jg = j + gap;
  250.  
  251.                 if ( special_case_0 )
  252.                     {
  253.                     if ( v[jg] == 0 )
  254.                         break;
  255.  
  256.                     else if ( v[j] != 0 && v[j] <= v[jg] )
  257.                         break;
  258.                     }
  259.  
  260.                 else if ( v[j] <= v[jg] )
  261.                     break;
  262.  
  263.                 k = v[j];
  264.                 v[j] = v[jg];
  265.                 v[jg] = k;
  266.                 }
  267.     }
  268.  
  269.  
  270. /* dataend - finish up a block of data declarations */
  271.  
  272. void dataend()
  273.     {
  274.     if ( datapos > 0 )
  275.         dataflush();
  276.  
  277.     /* add terminator for initialization; { for vi */
  278.     puts( "    } ;\n" );
  279.  
  280.     dataline = 0;
  281.     datapos = 0;
  282.     }
  283.  
  284.  
  285. /* dataflush - flush generated data statements */
  286.  
  287. void dataflush()
  288.     {
  289.     putchar( '\n' );
  290.  
  291.     if ( ++dataline >= NUMDATALINES )
  292.         {
  293.         /* Put out a blank line so that the table is grouped into
  294.          * large blocks that enable the user to find elements easily.
  295.          */
  296.         putchar( '\n' );
  297.         dataline = 0;
  298.         }
  299.  
  300.     /* Reset the number of characters written on the current line. */
  301.     datapos = 0;
  302.     }
  303.  
  304.  
  305. /* flexerror - report an error message and terminate */
  306.  
  307. void flexerror( msg )
  308. char msg[];
  309.     {
  310. #ifdef MPW
  311.     fprintf( stderr, "### %s - %s\n", program_name, msg );
  312. #else
  313.     fprintf( stderr, "%s: %s\n", program_name, msg );
  314. #endif
  315.     flexend( 1 );
  316.     }
  317.  
  318.  
  319. /* flexfatal - report a fatal error message and terminate */
  320.  
  321. void flexfatal( msg )
  322. char msg[];
  323.     {
  324. #ifdef MPW
  325.     fprintf( stderr, "### %s - Fatal internal error: %s\n", program_name, msg );
  326. #else
  327.     fprintf( stderr, "%s: fatal internal error, %s\n", program_name, msg );
  328. #endif
  329.     exit( 1 );
  330.     }
  331.  
  332.  
  333. /* lerrif - report an error message formatted with one integer argument */
  334.  
  335. void lerrif( msg, arg )
  336. char msg[];
  337. int arg;
  338.     {
  339.     char errmsg[MAXLINE];
  340.     (void) sprintf( errmsg, msg, arg );
  341.     flexerror( errmsg );
  342.     }
  343.  
  344.  
  345. /* lerrsf - report an error message formatted with one string argument */
  346.  
  347. void lerrsf( msg, arg )
  348. char msg[], arg[];
  349.     {
  350.     char errmsg[MAXLINE];
  351.  
  352.     (void) sprintf( errmsg, msg, arg );
  353.     flexerror( errmsg );
  354.     }
  355.  
  356.  
  357. /* htoi - convert a hexadecimal digit string to an integer value */
  358.  
  359. int htoi( str )
  360. Char str[];
  361.     {
  362.     unsigned int result;
  363.  
  364.     (void) sscanf( (char *) str, "%x", &result );
  365.  
  366.     return result;
  367.     }
  368.  
  369.  
  370. /* is_hex_digit - returns true if a character is a valid hex digit, false
  371.  *          otherwise
  372.  */
  373.  
  374. int is_hex_digit( ch )
  375. int ch;
  376.     {
  377.     if ( isdigit( ch ) )
  378.         return 1;
  379.  
  380.     switch ( clower( ch ) )
  381.         {
  382.         case 'a':
  383.         case 'b':
  384.         case 'c':
  385.         case 'd':
  386.         case 'e':
  387.         case 'f':
  388.             return 1;
  389.  
  390.         default:
  391.             return 0;
  392.         }
  393.     }
  394.  
  395.  
  396. /* line_directive_out - spit out a "# line" statement */
  397.  
  398. void line_directive_out( output_file )
  399. FILE *output_file;
  400.     {
  401.     if ( infilename && gen_line_dirs )
  402.         {
  403.         char directive[MAXLINE];
  404.         sprintf( directive, "# line %d \"%s\"\n", linenum, infilename );
  405.  
  406.         /* If output_file is nil then we should put the directive in
  407.          * the accumulated actions.
  408.          */
  409.         if ( output_file )
  410.             fputs( directive, output_file );
  411.         else
  412.             add_action( directive );
  413.         }
  414.     }
  415.  
  416.  
  417. /* mark_defs1 - mark the current position in the action array as
  418.  *               representing where the user's section 1 definitions end
  419.  *         and the prolog begins
  420.  */
  421. void mark_defs1()
  422.     {
  423.     defs1_offset = 0;
  424.     action_array[action_index++] = '\0';
  425.     action_offset = prolog_offset = action_index;
  426.     action_array[action_index] = '\0';
  427.     }
  428.  
  429.  
  430. /* mark_prolog - mark the current position in the action array as
  431.  *               representing the end of the action prolog
  432.  */
  433. void mark_prolog()
  434.     {
  435.     action_array[action_index++] = '\0';
  436.     action_offset = action_index;
  437.     action_array[action_index] = '\0';
  438.     }
  439.  
  440.  
  441. /* mk2data - generate a data statement for a two-dimensional array
  442.  *
  443.  * Generates a data statement initializing the current 2-D array to "value".
  444.  */
  445. void mk2data( value )
  446. int value;
  447.     {
  448.     if ( datapos >= NUMDATAITEMS )
  449.         {
  450.         putchar( ',' );
  451.         dataflush();
  452.         }
  453.  
  454.     if ( datapos == 0 )
  455.         /* Indent. */
  456.         fputs( "    ", stdout );
  457.  
  458.     else
  459.         putchar( ',' );
  460.  
  461.     ++datapos;
  462.  
  463.     printf( "%5d", value );
  464.     }
  465.  
  466.  
  467. /* mkdata - generate a data statement
  468.  *
  469.  * Generates a data statement initializing the current array element to
  470.  * "value".
  471.  */
  472. void mkdata( value )
  473. int value;
  474.     {
  475.     if ( datapos >= NUMDATAITEMS )
  476.         {
  477.         putchar( ',' );
  478.         dataflush();
  479.         }
  480.  
  481.     if ( datapos == 0 )
  482.         /* Indent. */
  483.         fputs( "    ", stdout );
  484.     else
  485.         putchar( ',' );
  486.  
  487.     ++datapos;
  488.  
  489.     printf( "%5d", value );
  490.     }
  491.  
  492.  
  493. /* myctoi - return the integer represented by a string of digits */
  494.  
  495. int myctoi( array )
  496. char array[];
  497.     {
  498.     int val = 0;
  499.  
  500.     (void) sscanf( array, "%d", &val );
  501.  
  502.     return val;
  503.     }
  504.  
  505.  
  506. /* myesc - return character corresponding to escape sequence */
  507.  
  508. Char myesc( array )
  509. Char array[];
  510.     {
  511.     Char c, esc_char;
  512.  
  513.     switch ( array[1] )
  514.         {
  515.         case 'b': return '\b';
  516.         case 'f': return '\f';
  517.         case 'n': return '\n';
  518.         case 'r': return '\r';
  519.         case 't': return '\t';
  520.  
  521. #ifdef __STDC__
  522.         case 'a': return '\a';
  523.         case 'v': return '\v';
  524. #else
  525.         case 'a': return '\007';
  526.         case 'v': return '\013';
  527. #endif
  528.  
  529.         case '0':
  530.         case '1':
  531.         case '2':
  532.         case '3':
  533.         case '4':
  534.         case '5':
  535.         case '6':
  536.         case '7':
  537.         case '8':
  538.         case '9':
  539.             { /* \<octal> */
  540.             int sptr = 1;
  541.  
  542.             while ( isascii( array[sptr] ) &&
  543.                 isdigit( array[sptr] ) )
  544.                 /* Don't increment inside loop control
  545.                  * because if isdigit() is a macro it might
  546.                  * expand into multiple increments ...
  547.                  */
  548.                 ++sptr;
  549.  
  550.             c = array[sptr];
  551.             array[sptr] = '\0';
  552.  
  553.             esc_char = otoi( array + 1 );
  554.  
  555.             array[sptr] = c;
  556.  
  557.             return esc_char;
  558.             }
  559.  
  560.         case 'x':
  561.             { /* \x<hex> */
  562.             int sptr = 2;
  563.  
  564.             while ( isascii( array[sptr] ) &&
  565.                 is_hex_digit( (char) array[sptr] ) )
  566.                 /* Don't increment inside loop control
  567.                  * because if isdigit() is a macro it might
  568.                  * expand into multiple increments ...
  569.                  */
  570.                 ++sptr;
  571.  
  572.             c = array[sptr];
  573.             array[sptr] = '\0';
  574.  
  575.             esc_char = htoi( array + 2 );
  576.  
  577.             array[sptr] = c;
  578.  
  579.             return esc_char;
  580.             }
  581.  
  582.         default:
  583.             return array[1];
  584.         }
  585.     }
  586.  
  587.  
  588. /* otoi - convert an octal digit string to an integer value */
  589.  
  590. int otoi( str )
  591. Char str[];
  592.     {
  593.     unsigned int result;
  594.  
  595.     (void) sscanf( (char *) str, "%o", &result );
  596.     return result;
  597.     }
  598.  
  599.  
  600. /* readable_form - return the the human-readable form of a character
  601.  *
  602.  * The returned string is in static storage.
  603.  */
  604.  
  605. char *readable_form( c )
  606. register int c;
  607.     {
  608.     static char rform[10];
  609.  
  610.     if ( (c >= 0 && c < 32) || c >= 127 )
  611.         {
  612.         switch ( c )
  613.             {
  614.             case '\b': return "\\b";
  615.             case '\f': return "\\f";
  616.             case '\n': return "\\n";
  617.             case '\r': return "\\r";
  618.             case '\t': return "\\t";
  619.  
  620. #ifdef __STDC__
  621.             case '\a': return "\\a";
  622.             case '\v': return "\\v";
  623. #endif
  624.  
  625.             default:
  626.                 (void) sprintf( rform, "\\%.3o",
  627.                         (unsigned int) c );
  628.                 return rform;
  629.             }
  630.         }
  631.  
  632.     else if ( c == ' ' )
  633.         return "' '";
  634.  
  635.     else
  636.         {
  637.         rform[0] = c;
  638.         rform[1] = '\0';
  639.  
  640.         return rform;
  641.         }
  642.     }
  643.  
  644.  
  645. /* reallocate_array - increase the size of a dynamic array */
  646.  
  647. void *reallocate_array( array, size, element_size )
  648. void *array;
  649. int size, element_size;
  650.     {
  651.     register void *new_array;
  652.  
  653.     /* Same worry as in allocate_array(): */
  654.     if ( size * element_size <= 0 )
  655.         flexfatal(
  656.             "attempt to increase array size by less than 1 byte" );
  657.  
  658.     new_array = flex_realloc( array, size * element_size );
  659.  
  660.     if ( new_array == NULL )
  661.         flexfatal( "attempt to increase array size failed" );
  662.  
  663.     return new_array;
  664.     }
  665.  
  666.  
  667. /* skelout - write out one section of the skeleton file
  668.  *
  669.  * Description
  670.  *    Copies skelfile or skel array to stdout until a line beginning with
  671.  *    "%%" or EOF is found.
  672.  */
  673. void skelout()
  674.     {
  675.     char buf_storage[MAXLINE];
  676.     char *buf = buf_storage;
  677.     int do_copy = 1;
  678.  
  679.     /* Loop pulling lines either from the skelfile, if we're using
  680.      * one, or from the skel[] array.
  681.      */
  682.     while ( skelfile ?
  683.         (fgets( buf, MAXLINE, skelfile ) != NULL) :
  684.         ((buf = skel[skel_ind++]) != 0) )
  685.         { /* copy from skel array */
  686.         if ( buf[0] == '%' )
  687.             { /* control line */
  688.             switch ( buf[1] )
  689.                 {
  690.                 case '%':
  691.                     return;
  692.  
  693.                 case '+':
  694.                     do_copy = C_plus_plus;
  695.                     break;
  696.  
  697.                 case '-':
  698.                     do_copy = ! C_plus_plus;
  699.                     break;
  700.  
  701.                 case '*':
  702.                     do_copy = 1;
  703.                     break;
  704.  
  705.                 default:
  706.                     flexfatal(
  707.                         "bad line in skeleton file" );
  708.                 }
  709.             }
  710.  
  711.         else if ( do_copy )
  712.             {
  713.             if ( skelfile )
  714.                 /* Skeleton file reads include final
  715.                  * newline, skel[] array does not.
  716.                  */
  717.                 fputs( buf, stdout );
  718.             else
  719.                 printf( "%s\n", buf );
  720.             }
  721.         }
  722.     }
  723.  
  724.  
  725. /* transition_struct_out - output a yy_trans_info structure
  726.  *
  727.  * outputs the yy_trans_info structure with the two elements, element_v and
  728.  * element_n.  Formats the output with spaces and carriage returns.
  729.  */
  730.  
  731. void transition_struct_out( element_v, element_n )
  732. int element_v, element_n;
  733.     {
  734.     printf( "%7d, %5d,", element_v, element_n );
  735.  
  736.     datapos += TRANS_STRUCT_PRINT_LENGTH;
  737.  
  738.     if ( datapos >= 75 )
  739.         {
  740.         putchar( '\n' );
  741.  
  742.         if ( ++dataline % 10 == 0 )
  743.             putchar( '\n' );
  744.  
  745.         datapos = 0;
  746.         }
  747.     }
  748.  
  749.  
  750. /* The following is only needed when building flex's parser using certain
  751.  * broken versions of bison.
  752.  */
  753. void *yy_flex_xmalloc( size )
  754. int size;
  755.     {
  756.     void *result = flex_alloc( size );
  757.  
  758.     if ( ! result  )
  759.         flexfatal( "memory allocation failed in yy_flex_xmalloc()" );
  760.  
  761.     return result;
  762.     }
  763.  
  764.  
  765. /* zero_out - set a region of memory to 0
  766.  *
  767.  * Sets region_ptr[0] through region_ptr[size_in_bytes - 1] to zero.
  768.  */
  769.  
  770. void zero_out( region_ptr, size_in_bytes )
  771. char *region_ptr;
  772. int size_in_bytes;
  773.     {
  774.     register char *rp, *rp_end;
  775.  
  776.     rp = region_ptr;
  777.     rp_end = region_ptr + size_in_bytes;
  778.  
  779.     while ( rp < rp_end )
  780.         *rp++ = 0;
  781.     }
  782.